﻿using System;
using System.Web.Http.Filters;
using System.Web.Http.Controllers;
using Caramel;

namespace Caramel.Web.Http.Filters
{
	public class ReadOnlyUnitOfWorkFilterAttribute : ActionFilterAttribute
	{
		private IUnitOfWork UnitOfWork { get; set; }

		public override void OnActionExecuting(HttpActionContext actionContext)
		{
			this.UnitOfWork = Caramel.UnitOfWork.BeginReadOnly();
		}

		public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
		{
			this.UnitOfWork.Dispose();
		}
	}
}